# -*- coding: UTF-8 -*-
# 作者： 磊
# @Time：2022/5/4
# coding=utf-8
import json
import random
import pytest as pytest
from Demo03.common.request_util import RequestsUtil

'''
1、请求方式的封装RequestsUtil()
2、去掉cookies，加上session，保持回话
'''
class TestSecondRequest4:
    access_token=""
    def test_get_toke(self):
        url="https://api.weixin.qq.com/cgi-bin/token"
        params={
            'appid': 'wx74a8627810cfa308',
            'secret': 'e40a02f9d79a8097df497e6aaf93ab80',
            'grant_type': 'client_credential'
        }

        res=RequestsUtil().send_request(method="get",url=url,params=params)
        return_data=res.json()

        # 返回字典格式数据
        print("返回字典格式数据：",return_data)
        TestSecondRequest4.access_token=return_data['access_token']

    def test_post_edit(self):
        url="https://api.weixin.qq.com/cgi-bin/tags/update?access_token="+TestSecondRequest4.access_token+""

        data = {"tag":{"id":108,"name":"ms"+str(random.randint(10000,99999))}}
        #application/x-www-form-urlencoded   data传参  这里是嵌套的，不行
        #application/json                    json传参
        # json.dumps()  把json格式转换成字符串
        # json.loads()  把字符串转换成字典
        res=RequestsUtil().send_request(method="get",url=url,data=json.dumps(data)) # 这个的data可以转换成json.dumps(data)，在通过data进行传参


    def test_file_up(self):
        url="https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token="+TestSecondRequest4.access_token+""
        data={
            "media":open(r"E:\shu.png","rb")
        }
        res = RequestsUtil().send_request(method="get", url=url, files=data)


if __name__ == '__main__':
        pytest.main(['-vs',"test05.py"])




